﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
/// Summary description for MyCart
/// </summary>
public class MyCart
{
    private readonly List<CartItem> _items;

    public List<CartItem> Items
    {
        get { return _items; }
    } 
    
	public MyCart()
	{
        _items = new List<CartItem>();
	}

    public void AddItem(CartItem item)
    {
        int indexofExistedItem = -1;
        for (int i = 0; i < _items.Count; i++)
        {
            if (_items[i].ProductID == item.ProductID)
            {
                indexofExistedItem = i;
                break;
            }
        }
        if (indexofExistedItem != -1)
        {
            _items[indexofExistedItem].Quantity += item.Quantity;
        }
        else
            _items.Add(item);
    }

    public void UpdateItem(int productID, int quantity)
    {
        for (int i = 0; i < _items.Count; i++)
        {
            if (_items[i].ProductID == productID)
            {
                _items[i].Quantity = quantity;
                break;
            }
        }
    }

    public void RemoveItem(int productID)
    {
        for (int i = 0; i < _items.Count; i++)
        {
            if (_items[i].ProductID == productID)
            {
                _items.RemoveAt(i);
                break;
            }
        }
    }
    public double GetTotalPrice()
    {
        double s = 0;
        foreach (CartItem c in _items)
        {
            s += c.Cost;
        }
        return s;
    }
    public int GetNumberofItem()
    {
        int quantity = 0;
        foreach (CartItem c in _items)
        {
            quantity += c.Quantity;
        }
        return quantity;
    }
}
